package com.potato369.cryptEureka.controller;

import com.potato369.cryptEureka.common.api.CommonResult;
import com.potato369.cryptEureka.domain.Brand;
import com.potato369.cryptEureka.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 品牌管理Controller
 * Created by macro on 2019/4/19.
 */
@Slf4j
@Api(tags = "PmsBrandController", description = "品牌管理")
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    private BrandService brandService;

    @Autowired
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }

    @ApiOperation("获取所有品牌列表")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    @GetMapping(value = "listAll")
    public CommonResult<List<Brand>> getBrandList() {
        return CommonResult.success(this.brandService.listAllBrand());
    }

    @ApiOperation("添加品牌")
    @PreAuthorize("hasAuthority('pms:brand:create')")
    @PostMapping(value = "/create")
    public CommonResult<?> createBrand(@RequestBody Brand pmsBrand) {
        CommonResult<?> commonResult;
        int count = this.brandService.createBrand(pmsBrand);
        if (count == 1) {
            commonResult = CommonResult.success(pmsBrand);
            log.info("createBrand success:{}", pmsBrand);
        } else {
            commonResult = CommonResult.failed("操作失败");
            log.info("createBrand failed:{}", pmsBrand);
        }
        return commonResult;
    }

    @ApiOperation("更新指定id品牌信息")
    @PreAuthorize("hasAuthority('pms:brand:update')")
    @PostMapping(value = "/update/{id}")
    public CommonResult<?> updateBrand(@ApiParam(name = "品牌id", required = true, example = "20") @PathVariable(name = "id", required = true) Long id,
                                    @RequestBody Brand pmsBrandDto, BindingResult result) {
        CommonResult<?> commonResult;
        if (result.hasErrors() && !Objects.isNull(result.getFieldError())) {
            return CommonResult.failed(result.getFieldError().getDefaultMessage());
        }
        int count = this.brandService.updateBrand(id, pmsBrandDto);
        if (count == 1) {
            commonResult = CommonResult.success(pmsBrandDto);
            log.info("updateBrand success:{}", pmsBrandDto);
        } else {
            commonResult = CommonResult.failed("操作失败");
            log.info("updateBrand failed:{}", pmsBrandDto);
        }
        return commonResult;
    }

    @ApiOperation("删除指定id的品牌")
    @PreAuthorize("hasAuthority('pms:brand:delete')")
    @GetMapping(value = "/delete/{id}")
    public CommonResult<?> deleteBrand(@ApiParam(name = "品牌id", required = true, example = "20") @PathVariable(name = "id", required = true) Long id) {
        int count = this.brandService.deleteBrand(id);
        if (count == 1) {
            log.info("deleteBrand success :id={}", id);
            return CommonResult.success(null);
        } else {
            log.info("deleteBrand failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("分页查询品牌列表")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    @GetMapping(value = "/pageList")
    public CommonResult<?> listBrand(@ApiParam(name = "页码", defaultValue = "1", example = "1") @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                     @ApiParam(name = "每页数量", defaultValue = "3", example = "3") @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize) {
        return CommonResult.success(this.brandService.listBrand(pageNum, pageSize));
    }

    @ApiOperation("获取指定id的品牌详情")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    @GetMapping(value = "/{id}")
    public CommonResult<?> brand(@ApiParam(name = "品牌id", required = true) @PathVariable(name = "id", required = true) Long id) {
        return CommonResult.success(this.brandService.getBrand(id));
    }
}
